package com.snap.vseries.fragment;

import android.annotation.SuppressLint;
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.support.constraint.ConstraintLayout;
import android.view.View;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.LinearLayout;
import android.widget.RadioGroup;
import android.widget.TextView;

import com.snap.vseries.R;
import com.snap.vseries.base.BaseFragment;
import com.snap.vseries.bean.ExpInfo;
import com.snap.vseries.bean.HoleInfo;
import com.snap.vseries.manager.FileManager;
import com.snap.vseries.model.DBManager;
import com.snap.vseries.ui.ExpNewExperimentActivity;
import com.snap.vseries.ui.widget.DialogView;
import com.snap.vseries.utils.FileUtils;
import com.snap.vseries.utils.ToastUtils;

import java.util.Objects;

import butterknife.BindView;
import butterknife.OnClick;

import static com.snap.vseries.fragment.NewExperimentTemplateFragment.CUSTOM_SETTING;

@SuppressLint("ValidFragment")
public class NewExperimentInfoSettingFragment extends BaseFragment implements CompoundButton.OnCheckedChangeListener {
    @BindView(R.id.exp_name_tv)
    TextView expNameTv;
    @BindView(R.id.fam_cb)
    CheckBox famCb;
    @BindView(R.id.vic_cb)
    CheckBox vicCb;
    @BindView(R.id.rox_cb)
    CheckBox roxCb;
    @BindView(R.id.cy5_cb)
    CheckBox cy5Cb;
    @BindView(R.id.analysis_rg)
    RadioGroup analysisRg;
    @BindView(R.id.sample_name_tv)
    TextView sampleNameTv;
    @BindView(R.id.template_ll)
    LinearLayout templateLl;
    @BindView(R.id.operator_tv)
    TextView operatorTv;
    @BindView(R.id.operator_cl)
    ConstraintLayout operatorCl;
    @BindView(R.id.rg)
    RadioGroup rg;
    @BindView(R.id.fam_tv)
    TextView famTv;
    @BindView(R.id.vic_tv)
    TextView vicTv;
    @BindView(R.id.rox_tv)
    TextView roxTv;
    @BindView(R.id.cy5_tv)
    TextView cy5Tv;
    private ExpInfo expInfo;
    private HoleInfo holeInfo;

    @SuppressLint("ValidFragment")
    public NewExperimentInfoSettingFragment(ExpInfo expInfo) {
        this.expInfo = expInfo;
    }

    @Override
    protected int layoutResId() {
        return R.layout.fragment_new_experiment_info_setting;
    }


    @Override
    public void onViewCreated(@NonNull View view, @Nullable Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);
        setIndex();
        initViews();
    }


    @OnClick({R.id.btn_next, R.id.sample_name_tv, R.id.exp_name_tv, R.id.fam_tv, R.id.vic_tv, R.id.rox_tv, R.id.cy5_tv})
    public void click(View view) {
        switch (view.getId()) {
            case R.id.sample_name_tv:
                setSampleName();
                break;
            case R.id.exp_name_tv:
                rename();
                break;
            case R.id.btn_next:
                //自定义设置模式下才需要勾选
                if (CUSTOM_SETTING.equals(expInfo.getTestProject())) {
                    if (!isChooseTestingItem()) {
                        ToastUtils.get().showText("没有选择任何检测项目");
                        return;
                    }
                    ExpNewExperimentActivity activity = (ExpNewExperimentActivity) getActivity();
                    Objects.requireNonNull(activity).showFragment(activity.index + 1);
                } else {
                    ExpNewExperimentActivity activity = (ExpNewExperimentActivity) getActivity();
                    Objects.requireNonNull(activity).showFragment(activity.index + 2);
                }
                break;

            case R.id.fam_tv:
                new DialogView(getContext()).setTitle("请输入自定义通道名称")
                        .setEditContent(expInfo.getFamName() == null ? "" : expInfo.getFamName())
                        .setTextLimit(10)
                        .setOnClickListener(new DialogView.OnSureListener() {
                            @Override
                            public void onSureClick(String text) {
                                ((TextView) view).setText(text);
                                expInfo.setFamName(text);
                            }
                        }).show();
                break;
            case R.id.vic_tv:
                new DialogView(getContext()).setTitle("请输入自定义通道名称")
                        .setEditContent(expInfo.getVicName() == null ? "" : expInfo.getVicName())
                        .setTextLimit(10)
                        .setOnClickListener(new DialogView.OnSureListener() {
                            @Override
                            public void onSureClick(String text) {
                                ((TextView) view).setText(text);
                                expInfo.setVicName(text);
                            }
                        }).show();
                break;
            case R.id.rox_tv:
                new DialogView(getContext()).setTitle("请输入自定义通道名称")
                        .setEditContent(expInfo.getRoxName() == null ? "" : expInfo.getRoxName())
                        .setTextLimit(10)
                        .setOnClickListener(new DialogView.OnSureListener() {
                            @Override
                            public void onSureClick(String text) {
                                ((TextView) view).setText(text);
                                expInfo.setRoxName(text);
                            }
                        }).show();
                break;
            case R.id.cy5_tv:
                new DialogView(getContext()).setTitle("请输入自定义通道名称")
                        .setTextLimit(10)
                        .setEditContent(expInfo.getCy5Name() == null ? "" : expInfo.getCy5Name())
                        .setOnClickListener(new DialogView.OnSureListener() {
                            @Override
                            public void onSureClick(String text) {
                                ((TextView) view).setText(text);
                                expInfo.setCy5Name(text);
                            }
                        }).show();
                break;
            default:
                break;
        }
    }

    private void initViews() {
        //模板启动，直接操作
        if (!CUSTOM_SETTING.equals(expInfo.getTestProject())) {
            operatorCl.setVisibility(View.VISIBLE);
            operatorTv.setVisibility(View.VISIBLE);
            templateLl.setVisibility(View.GONE);
            initTemplate();
        } else {
            //自定义设置
            if (expInfo.getHoleInfo() == null) {
                expInfo.setHoleInfo(new HoleInfo());
                holeInfo = expInfo.getHoleInfo();
                holeInfo.setFamEnable(true);
                holeInfo.setVicEnable(true);
                holeInfo.setRoxEnable(true);
                holeInfo.setCy5Enable(true);
            }
            operatorCl.setVisibility(View.GONE);
            operatorTv.setVisibility(View.GONE);
            templateLl.setVisibility(View.VISIBLE);
            //分析方法默认选中自动
            analysisRg.check(R.id.auto_rb);
            initChannel();
        }
        expNameTv.setText(expInfo.getName());
    }

    private boolean isChooseTestingItem() {
        boolean isChooseTestingItem = false;
        for (int i = 0; i < rg.getChildCount(); i++) {
            if (((CheckBox) rg.getChildAt(i)).isChecked()) {
                isChooseTestingItem = true;
                break;
            }
        }
        return isChooseTestingItem;
    }

    private void setSampleName() {
        final DialogView dialogView = new DialogView(getContext());
        dialogView.setTitle(getString(R.string.fill_sample_name))
                .setEditContent("")
                .setOnClickListener(content -> {
                    sampleNameTv.setText(content);
                    expInfo.setSampleName(content);
                    dialogView.dismiss();
                }).show();
    }

    private void initTemplate() {
        ExpInfo project = FileManager.getInstance().getTempDataByName(expInfo.getTestProject());
        expInfo.setProgram(project.getProgram());
        expInfo.setHoleInfo(project.getHoleInfo());
        expInfo.setFamName(project.getFamName());
        expInfo.setVicName(project.getVicName());
        expInfo.setRoxName(project.getRoxName());
        expInfo.setCy5Name(project.getCy5Name());
    }

    @Override
    public void onHiddenChanged(boolean hidden) {
        super.onHiddenChanged(hidden);
        if (!hidden) {
            setIndex();
            initViews();
        }
    }

    private void setIndex() {
        ExpNewExperimentActivity activity = (ExpNewExperimentActivity) getActivity();
        Objects.requireNonNull(activity).index = 1;
    }

    private void initChannel() {
        famCb.setChecked(holeInfo.isFamEnable());
        initFam(holeInfo.isFamEnable());
        vicCb.setChecked(holeInfo.isVicEnable());
        initVic(holeInfo.isVicEnable());
        roxCb.setChecked(holeInfo.isRoxEnable());
        initRox(holeInfo.isRoxEnable());
        cy5Cb.setChecked(holeInfo.isCy5Enable());
        initCy5(holeInfo.isCy5Enable());
        famCb.setOnCheckedChangeListener(this);
        vicCb.setOnCheckedChangeListener(this);
        roxCb.setOnCheckedChangeListener(this);
        cy5Cb.setOnCheckedChangeListener(this);
    }


    private void initCy5(boolean enable) {
        cy5Tv.setEnabled(enable);
        cy5Tv.setText(enable ? (expInfo == null ? "" : expInfo.getCy5Name()) : "请先启用通道");
    }

    private void initRox(boolean enable) {
        roxTv.setEnabled(enable);
        roxTv.setText(enable ? (expInfo == null ? "" : expInfo.getRoxName()) : "请先启用通道");
    }

    private void initVic(boolean enable) {
        vicTv.setEnabled(enable);
        vicTv.setText(enable ? (expInfo == null ? "" : expInfo.getVicName()) : "请先启用通道");
    }

    private void initFam(boolean enable) {
        famTv.setEnabled(enable);
        famTv.setText(enable ? (expInfo == null ? "" : expInfo.getFamName()) : "请先启用通道");
    }

    @Override
    public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
        switch (buttonView.getId()) {
            case R.id.fam_cb:
                holeInfo.setFamEnable(isChecked);
                initFam(isChecked);
                break;
            case R.id.vic_cb:
                holeInfo.setVicEnable(isChecked);
                initVic(isChecked);
                break;
            case R.id.rox_cb:
                holeInfo.setRoxEnable(isChecked);
                initRox(isChecked);
                break;
            case R.id.cy5_cb:
                holeInfo.setCy5Enable(isChecked);
                initCy5(isChecked);
                break;
            default:
                break;
        }
    }

    private void rename() {
        final DialogView dialogView = new DialogView(getContext());
        dialogView.setTitle(getString(R.string.exp_name_edit_tips))
                .setEditContent(expInfo.getName())
                .setClickSureDismissDialog(false)
                .setOnClickListener(content -> {
                    if (!FileUtils.checkFileName(content)) {
                        ToastUtils.get().showText("文件名称格式不正确!");
                        return;
                    }
                    DBManager.get().getExpByName(content, entity -> {
                        if (entity == null) {
                            expNameTv.setText(content);
                            expInfo.setName(content);
                            dialogView.dismiss();
                        } else {
                            ToastUtils.get().showText("实验已存在，请重新命名！");
                        }
                    });
                })
                .show();
    }
}
